package java_thinking.unit_18_IO._18_6;

import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * @Desc:   使用 RandomAccessFile 读取基本类型和UTF-8
 *          1.RandomAccessFile 不支持装饰，所以不能将其与InputStream及OutputStream子类的任何部分结合起来；
 * @author: cww
 * @DateTime: 2019/5/15 8:52
 */

public class UsingRandomAccessFile {
    static String file = "src/maindemo/java/java_thinking/unit_18_IO/_18_6/UsingRandomAccessFile.txt";

    /**
     * 打开一个文件
     */
    static void display() throws IOException {
        RandomAccessFile rf = new RandomAccessFile(file, "r");
        for (int i = 0; i < 7; i++) {
            System.out.println("Value " + i + ": " + rf.readDouble());
        }
        System.out.println(rf.readUTF());
        rf.close();
    }

    public static void main(String[] args) throws IOException {
        RandomAccessFile rf = new RandomAccessFile(file, "rw");
        for (int i = 0; i < 7; i++) {
            rf.writeDouble(i*1.414);
        }
        rf.writeUTF("The end of file");
        rf.close();
        display();
        rf = new RandomAccessFile(file, "rw");
        //double以8个字节存储，所以，要插入第x个double就是x*8
        rf.seek(5*8);
        rf.writeDouble(47.0001);
        rf.close();
        display();
    }
}
